package com.trkj.controller.hjj;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.pojo.StaffInformation;
import com.trkj.service.hjj.CheckingService_lxl;
import com.trkj.vo.CheckingVo;
import com.trkj.vo.Result_AddRule_Result;

@Controller
public class CheckingController_lxl {
	@Autowired
	private CheckingService_lxl checkService;
	//mybatis分页
		@GetMapping("to_checkingfindall")
		public String to_1(){
				return "hjj/checking-result-set";
		}
		
	@RequestMapping("checkfindAll")
	@ResponseBody
	public Map checkfindAll(int page,int rows) {
		Page<JSONArray> page1 = PageHelper.startPage(page,rows); 
		checkService.findAllChecking();
        List<JSONArray> list  = page1.getResult(); 
        Map map = new HashMap<>();
		map.put("total", page1.getTotal());
		map.put("rows", list);
		return map;
	}
	//详情小表格下拉框查询
	@RequestMapping("nopage_checkfindAll")
	@ResponseBody
	public JSONArray checkfindnoPage(){
		return checkService.findAllChecking();
	}
	
	@RequestMapping("nopage_findAllAttend")
	@ResponseBody
	public JSONArray findAllAttend(){
		return checkService.findAllAttend();
	}
	
	@RequestMapping("nopage_findAllCalcluaterule")
	@ResponseBody
	public JSONArray findAllCalcluaterule(){
		return checkService.findAllCalcluaterule();
	}
	@RequestMapping("is_to_checkbyname_like")
	@ResponseBody
	public Map findEmpMOHU(int page,int rows,String kqruleName){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		checkService.findKqByName(kqruleName);
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
	}

	//到详情页面
	@GetMapping("to_checkingdetails")
	public String to_2(Long kqruleId,Model m){
		//System.out.println(checkService.checkingByValue(kqruleId));
		m.addAttribute("list", checkService.checkingByValue(kqruleId));
		return "hjj/checking-result-details";
	}
	
	//编辑页面
	@GetMapping("to_checkingedit")
	public String to_3(Long kqruleId,CheckingVo checkvo,Model m){
		//System.out.println(checkService.checkingByValue(kqruleId));
		m.addAttribute("list", checkService.editByValue(kqruleId, checkvo));
		return "hjj/checking-result-edit";
	}
	
	/**
	 * 编辑
	 * @param checkvo
	 * @return
	 */
	@RequestMapping("check_rule_edit")
	@ResponseBody
	public Result_AddRule_Result editChecking(CheckingVo checkvo){
		try {
			checkService.editChecking(checkvo.getKaoqinrules().getKqruleId(), checkvo);
			return Result_AddRule_Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 详情页面查询
	 * @param kqruleId
	 * @return
	 */
	@RequestMapping("checkxqmingxi")
	@ResponseBody
	public List xqChecking(Long kqruleId) {
		//Page<JSONArray> page1 = PageHelper.startPage(page,rows); 
		
		System.out.println(checkService.xqChecking(kqruleId)+""+kqruleId);
       
		return checkService.xqChecking(kqruleId);
	}
	
	//去到新增页面
	@GetMapping("to_checking-inset")
	public String to_3(){
		return "hjj/checking-result-add";
	}
	
	/**
	 * 新增
	 * @param attruId 考勤结果id
	 * @param kqruleId 考勤规则id
	 * @param calcluateruleId 计算规则id
	 * @param kqruleName 考勤规则名称
	 * @param kqruleUp 考勤出勤方式（上班）
	 * @param kqruleDown 考勤出勤方式（下班）
	 * @param kqruleKg 忽略旷工
	 * @param kqruleMarks 备注
	 * @param attruName 考勤结果名称
	 * @param calcluateruleName 计算规则名称
	 * @param calcluateruleCount 计算规则一天工作小时数
	 */
	@RequestMapping("check_insert")
	@ResponseBody
	public Result_AddRule_Result addChecking(HttpSession session,CheckingVo checkvo){
		
		try {
			StaffInformation staff = (StaffInformation) session.getAttribute("staff");
			checkService.addChecking(staff.getStainName(), checkvo);		
			return Result_AddRule_Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	
	@RequestMapping("check_delete")
	@ResponseBody
	public Result_AddRule_Result delete(Long kqruleId){
		try {
			checkService.delete(kqruleId);
			return Result_AddRule_Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
}
